﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DragDropControl
{
    public partial class Form1 : Form
    {
        private bool isCreatingControl;
        public Form1()
        {
            InitializeComponent();
            //addMouseHoverHandler();
            addMouseDownHandler();
            designPnl.Paint += new PaintEventHandler(designPnl_Paint);
        }

        void designPnl_Paint(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Brushes.Black);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
            Graphics g = this.CreateGraphics();
            g.DrawLine(pen, new Point(0, 10), new Point(50, 10));

        }



        private void addMouseDownHandler()
        {
            textBoxImg.MouseDown += new MouseEventHandler(img_MouseDown);
            numberImg.MouseDown += new MouseEventHandler(img_MouseDown);
            dateTimeImg.MouseDown += new MouseEventHandler(img_MouseDown);
            checkBoxImg.MouseDown += new MouseEventHandler(img_MouseDown);
            radioImg.MouseDown += new MouseEventHandler(img_MouseDown);
            comboBoxImg.MouseDown += new MouseEventHandler(img_MouseDown);

            textBoxImg.MouseUp += new MouseEventHandler(img_MouseUp);
            numberImg.MouseUp += new MouseEventHandler(img_MouseUp);
            dateTimeImg.MouseUp += new MouseEventHandler(img_MouseUp);
            checkBoxImg.MouseUp += new MouseEventHandler(img_MouseUp);
            radioImg.MouseUp += new MouseEventHandler(img_MouseUp);
            comboBoxImg.MouseUp += new MouseEventHandler(img_MouseUp);
        }


        private void img_MouseDown(object sender, MouseEventArgs e)
        {
            isCreatingControl = true;
        }

        private void img_MouseUp(object sender, MouseEventArgs e)
        {
            if (isCreatingControl == false) return;
            isCreatingControl = false;
            PictureBox currentPic = (PictureBox)sender;
            int newX = e.X + currentPic.Location.X - designPnl.Location.X;
            int newY = e.Y + currentPic.Location.Y - designPnl.Location.Y;

            if (mouseUpInCorrectLocation(newX, newY))
            {
                
                Control creatingControl = getDragControl(currentPic);
                if (creatingControl != null)
                {

                    addControlToDesignPanel(creatingControl, newX, newY);
                }
            }
        }

        private bool mouseUpInCorrectLocation(int X, int Y)
        {
            if (X >= 0 && X < designPnl.Size.Width && Y > 0 && Y < designPnl.Size.Height)
            {
                return true;
            }
            return false;
        }


        private void addControlToDesignPanel(Control creatingControl, int newX, int newY)
        {
            designPnl.Controls.Add(creatingControl);
            creatingControl.Location = new Point(newX, newY);
        }

        private Control getDragControl(PictureBox pic)
        {
            if (pic == textBoxImg)
            {
                return new TextBox();
            }
            else if (pic == numberImg)
            {
                return new TextBox();
            }
            else if (pic == checkBoxImg)
            {
                return new CheckBox();
            }
            else if (pic == radioImg)
            {
                return new RadioButton();
            }
            else if (pic == dateTimeImg)
            {
                return new DateTimePicker();
            }
            else if (pic == comboBoxImg)
            {
                return new ComboBox();
            }
            else return null;
        }



        private void addMouseHoverHandler()
        {
            textBoxImg.MouseHover += new EventHandler(img_MouseHover);
            numberImg.MouseHover += new EventHandler(img_MouseHover);
            dateTimeImg.MouseHover += new EventHandler(img_MouseHover);
            checkBoxImg.MouseHover += new EventHandler(img_MouseHover);
            radioImg.MouseHover += new EventHandler(img_MouseHover);
            comboBoxImg.MouseHover += new EventHandler(img_MouseHover);

            textBoxImg.MouseLeave += new EventHandler(img_MouseLeave);
            numberImg.MouseLeave += new EventHandler(img_MouseLeave);
            dateTimeImg.MouseLeave += new EventHandler(img_MouseLeave);
            checkBoxImg.MouseLeave += new EventHandler(img_MouseLeave);
            radioImg.MouseLeave += new EventHandler(img_MouseLeave);
            comboBoxImg.MouseLeave += new EventHandler(img_MouseLeave);
        }

        void img_MouseLeave(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            pic.BorderStyle = BorderStyle.None;
        }

        void img_MouseHover(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            pic.BorderStyle = BorderStyle.Fixed3D;
        }

        

    }
}
